const {executeUpdate, executeInsert, executeSelect, executeCustom} = require("./class");
//预设的sql语句 搭配自定义sql执行函数 execute(sql) 预设的查询语句
// const knex = require('Knex') //给我推荐了一个便捷操作mysql的·库 …………

module.exports = db = {
    Find: async function find(tableName, columName, value, num, size) {
        let sql;
        if (num && size) {
            sql = `select *
                   from ${tableName}
                   where ${columName} like '%${value}%'
                   limit ${num * size - size},${size}`
        } else {
            sql = `select *
                   from ${tableName}
                   where ${columName} like '%${value}%'`
        }
        //模糊查询功能
        //传入表名，列名和 列值 来筛选对应的内容 模糊查询
        try {
            return await executeSelect(sql)
        } catch (e) {
            throw e
        }

    },
    ExactFind: async function exactFind(tableName, columnName, value) {
        //精确查询功能
        //传入表名，列名和 列值 来筛选对应的内容 精确查询
        try {
            return executeSelect(`select *
                                  from ${tableName}
                                  where ${columnName} = '${value}'`)

        } catch (e) {
            console.log(e.message)
        }
    },
    Add: async function add(tableName, object) {
        // 添加数据 功能
        //可以使用占位符
        // insert into ? set ? ; [table,obj] 需测试
        try {
            // debugger
            return await executeInsert(tableName, object)
            // console.log(res);
        } catch (e) {
            throw e
        }
    },
    /**
     *
     * @param tableName { string } table name
     * @param object {Object } data target
     * @param key { string} where , field name
     * @param value {string } where , field value
     * @return {Promise<unknown>}
     * @constructor
     */
    Update: async function update(tableName, object, key, value) {

        //可以使用占位符 // 更新功能
        // update [table] set [column] =? where [column] =? ;
        try {
            return await executeUpdate(tableName, object, key, value)
            // console.log(res);
        } catch (e) {
            throw e
        }
    },
    executeCustom: executeCustom
}
